package com.inspectortime.domain.billing;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.inspectortime.common.PersistentEntityBase;
import com.inspectortime.timestamp.Timestamped;

@Entity
@Table(name = "INVOICE")
public class Invoice extends PersistentEntityBase implements Timestamped {
	
	private Date createdTime;
	private Date lastModifiedTime;
	private Date invoicePeriodStartDate;
	private Date invoicePeriodEndDate;

	private String companyId;

	private List<InvoiceLineItem> lineItems = new ArrayList<InvoiceLineItem>();

	public double calculateInvoiceAmount() {
		double amount = 0;
		for (InvoiceLineItem line : this.getLineItems()) {
			amount += line.getAmount();
		}
		return amount;
	}

	@Column( nullable=false, length = 32 )
	public String getCompanyId() {
		return companyId;
	}

	public Date getCreatedTime() {
		return createdTime;
	}

	public Date getInvoicePeriodEndDate() {
		return invoicePeriodEndDate;
	}
	public Date getInvoicePeriodStartDate() {
		return invoicePeriodStartDate;
	}	
	public Date getLastModifiedTime() {
		return lastModifiedTime;
	}
	
	@OneToMany(targetEntity = InvoiceLineItem.class, cascade = { CascadeType.ALL })
	public List<InvoiceLineItem> getLineItems() {
		return lineItems;
	}
	
	public void setCompanyId(String inspectorCompanyId) {
		this.companyId = inspectorCompanyId;
	}
	public void setCreatedTime(Date createdTime) {
		this.createdTime = createdTime;
	}
	public void setInvoicePeriodEndDate(Date invoicePeriodEndDate) {
		this.invoicePeriodEndDate = invoicePeriodEndDate;
	}
	public void setInvoicePeriodStartDate(Date invoicePeriodStartDate) {
		this.invoicePeriodStartDate = invoicePeriodStartDate;
	}
	
	public void setLastModifiedTime(Date lastModifiedTime) {
		this.lastModifiedTime = lastModifiedTime;
	}
	
	public void setLineItems(List<InvoiceLineItem> invoiceLineItem) {
		this.lineItems = invoiceLineItem;
	}
	
	public String toString() {
		return "id=" + this.getId() + ", companyId=" + companyId + ", start=" + invoicePeriodStartDate + ", end=" + invoicePeriodEndDate ;
	}
}
